package czy.demo.controller;


import czy.demo.pojo.Person;
import czy.demo.service.JdbcTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/simple")
public class JdbcTemplateController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private JdbcTemplateService jdbcTemplateService;

    /* 执行指定sql语句，不返回任何东西 */
    @PostMapping("/execute")
    public String execute(@RequestBody String sql){
        this.jdbcTemplate.execute(sql);
        return "SQL执行完毕";
    }

    @GetMapping("/query/{id}")
    public Person getPersonById(@PathVariable Integer id){
        Person result = null;
        //result = this.jdbcTemplateService.getOneByIdWithResultSetExtractor(id);
        //result = this.jdbcTemplateService.getOneByIdWithRowMapper(id);
        result = this.jdbcTemplateService.getOneByIdWithClass(id);
        return result;
    }

    @GetMapping("/query/all")
    public List<Person> getAllPerson(){
        return this.jdbcTemplateService.getAllPerson();
    }

    @PostMapping("/update")
    public int updatePerson(@RequestBody Person person){
        return this.jdbcTemplateService.updateOne(person);
    }

    @DeleteMapping("/delete/{id}")
    public String deletePerson(@PathVariable Integer id){
        this.jdbcTemplateService.deleteOne(id);
        return "删除Person成功";
    }



}
